package com.foreverwin.mes.meapi.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.foreverwin.mes.core.base.FrontPage;
import com.foreverwin.mes.meapi.mapper.OperationMapper;
import com.foreverwin.mes.meapi.model.Operation;
import com.foreverwin.mes.meapi.service.OperationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Locale;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Syngna
 * @since 2020-01-14
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class OperationServiceImpl extends ServiceImpl<OperationMapper, Operation> implements OperationService {


    @Autowired
    private OperationMapper operationMapper;

    @Override
    public Page<Operation> selectPage(FrontPage<Operation> frontPage, Operation operation) {
        EntityWrapper<Operation> queryWrapper = new EntityWrapper<>();
        queryWrapper.setEntity(operation);
        return super.selectPage(frontPage.getPagePlus(), queryWrapper);
    }

    @Override
    public List<Operation> selectList(Operation operation) {
        EntityWrapper<Operation> queryWrapper = new EntityWrapper<>();
        queryWrapper.setEntity(operation);
        return super.selectList(queryWrapper);
    }

    @Override
    public List<Operation> listByResource(String resourceBo) {
        return operationMapper.listByResource(resourceBo);
    }

    @Override
    public List<Operation> selectList(Wrapper<Operation> queryWrapper) {
        Locale locale = LocaleContextHolder.getLocale();
        return this.operationMapper.selectList(locale.getLanguage(), queryWrapper);
    }

    @Override
    public Operation selectByCode(String site, String code) {
        EntityWrapper<Operation> queryWrapper = new EntityWrapper<>();
        Operation operation = new Operation();
        operation.setSite(site);
        operation.setOperation(code);
        operation.setCurrentRevision("true");
        queryWrapper.setEntity(operation);
        List<Operation> operations = selectList(queryWrapper);
        return operations.isEmpty() ? null : operations.get(0);
    }
}